package com.qin.controller;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qin.entity.Book;
import com.qin.entity.BookCategory;
import com.qin.entity.Comment;
import com.qin.entity.User;
import com.qin.service.BookCategoryService;
import com.qin.service.BookService;
import com.qin.service.CommentService;
import com.qin.service.UserService;
import com.qin.service.impl.BookCategoryServiceImpl;
import com.qin.service.impl.BookServiceImpl;
import com.qin.service.impl.CommentServiceImpl;
import com.qin.service.impl.UserServiceImpl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;

/**
 * 来处理所有产品请求（其实也就只有图书）
 */
@WebServlet("/book/*")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private BookService bookService = new BookServiceImpl();
	private BookCategoryService bookCategoryService = new BookCategoryServiceImpl();
	private CommentService commentService = new CommentServiceImpl();
	private UserService userService = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		StaticLog.debug("欢迎来到图书页！");
		// 拿到请求的url地址,和请求content路径
		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();

		// 去掉*/category/前缀
		String replace = requestURI.replace(contextPath + "/book/", "");

		StaticLog.debug("要请求的分类是：{}", replace);
		
		if(!StrUtil.isEmpty(replace)) {
			// 查出所有的一级分类
			List<BookCategory> categoryByCLevel = bookCategoryService.queryBookCategoryByCLevel(1);
			// 放在请求域中
			request.setAttribute("categoryByCLevel", categoryByCLevel);
						
			// 得到id
			long bookId = Long.parseLong(replace);
			Book bookById = bookService.getBookById(bookId);
			
			// 修改图书的信息
			bookService.updateBook(bookId, new Book(bookById.getClick()+1) );
			
			// 把book放在请求域中
			request.setAttribute("book", bookById);
			
			// 查询所有评论
			List<Comment> queryCommentByBookId = commentService.queryCommentByBookId(bookId);
			// 按照时间，从近到远排序
			List<Comment> collect = queryCommentByBookId.stream().map(comment->{
				// 赋值用户名
				User queryUser = userService.queryUser(comment.getUserId());
				comment.setUserName(queryUser.getName());
				
				return comment;
			}).sorted(Comparator.comparing(Comment::getCreateTime)).collect(Collectors.toList());
			
			// 把所有评论放在请求域中
			request.setAttribute("comments", collect);
			
			// 转发
			request.getRequestDispatcher("/WEB-INF/book/product.jsp").forward(request, response);;
		
			return;
		}
		
		// 回首页，不作任何处理
		response.sendRedirect(contextPath);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
